package com.guo.boot.study.thread;

/**
 * @Auther Heart Guo
 * @Date 2021/12/23-9:15
 */
public class Race implements Runnable{
    private String winner;
    @Override
    public void run() {
        //设置一百步
        for (int i = 0; i <= 100; i++) {
            //模拟兔子睡觉
            if(Thread.currentThread().getName().equals("兔子")){
                try {
                    Thread.sleep((long)0.00000000001);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"跑了-----"+i+"步");
            boolean flag = gameOver(i);
            if(flag){
                break;
            }
        }

    }

    public boolean gameOver(int steps){
        if(winner!=null){
            return true;
        }
        if(steps>=100){
            winner = Thread.currentThread().getName();
            System.out.println("winner is"+winner);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Race race = new Race();
        new Thread(race,"兔子").start();
        new Thread(race,"乌龟").start();
    }

}
